package cn.signalpha.modules.com.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.excel.util.ExcelUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.DateUtil;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.modules.com.dto.WalletDTO;
import cn.signalpha.modules.com.entity.enums.WalletAmountTypeEnum;
import cn.signalpha.modules.com.entity.enums.WalletSourceEnum;
import cn.signalpha.modules.com.excel.UserWalletExcel;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.com.service.UserWalletService;
import cn.signalpha.modules.com.vo.UserWalletVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_USER_NAME + "/admin/user/wallet")
@Api(value = "用户钱包", tags = "用户钱包")
public class UserWalletController extends BaseController {

	private final UserService userService;
	private final UserWalletService userWalletService;

	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<UserWalletVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		return R.data(userWalletService.selectUserWalletPage(Condition.getPage(query), params));
	}

	@GetMapping("/excel")
	@ApiOperation(value = "导出excel", notes = "")
	public void excel(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		List<UserWalletExcel> list = userWalletService.selectUserWalletExcel(params);
		ExcelUtil.export(response, DateUtil.time(), StringPool.EMPTY, list, UserWalletExcel.class);
	}

	@GetMapping("/amount")
	@ApiOperation(value = "统计", notes = "")
	public R amount(@ApiIgnore @RequestParam Map<String, Object> params) {
		return R.data(userWalletService.sumByParams(params));
	}

	@PostMapping("/save")
	@ApiOperation(value = "充值", notes = "")
	public R save(@Valid @RequestBody WalletDTO walletDTO) {
		walletDTO.setSourceId(getUser().getUserId().toString());
		walletDTO.setSourceType(WalletSourceEnum.SYSTEM.getType());
		walletDTO.setRemark(walletDTO.getType() == 1 ? "系统增加" : "系统减少");
		if(walletDTO.getAmountType().equals(WalletAmountTypeEnum.MONEY.getType())){
			return R.status(userService.updateUserMoney(walletDTO));
		}else if(walletDTO.getAmountType().equals(WalletAmountTypeEnum.INTEGRAL.getType())){
			return R.status(userService.updateUserIntegral(walletDTO));
		}
		return R.fail("请选择正确的金额类型");
	}

}
